package com.james.wee.pwddic;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PwdCreator {
	static java.io.BufferedWriter bw = null;

	public PwdCreator(String path) throws IOException {
		File file = new File(path);
		bw = new java.io.BufferedWriter(new FileWriter(file, true));
	}

	public static void close() throws IOException {
		bw.close();
	}

	// private static char[] a = { 'q', 'a', 'z', 'w', 's', 'x', 'e', 'd', 'c',
	// 'r', 'f', 'v', 't', 'g', 'b', 'y', 'h', 'n', 'u', 'j', 'm', 'i',
	// 'k', 'o', 'l', 'p', '0', '1', '2', '3', '4', '5', '6', '7', '8',
	// '9', 'Q', 'A', 'Z', 'W', 'S', 'X', 'E', 'D', 'C', 'R', 'F', 'V',
	// 'T', 'G', 'B', 'Y', 'H', 'N', 'U', 'J', 'M', 'I', 'K', 'O', 'P',
	// 'L', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_',
	// '+', '=', '-', '`', ',', '.', '/', '<', '>', '?', '|', '\\' };
	private static char[] a = { '0', '1', '2', '3', '4', '5', '6' };
	private static String root = null;
	private static long slens = 0;
	private static long elens = 0;
	static long start = 0;
	static List<String> pwdlist = new ArrayList<String>(2048);

	private void select(int k, int swap) {

		char tmp = a[0];
		a[0] = a[swap];
		a[swap] = tmp;
		char[] result = new char[k];
		T t = new T();
		root = t.toString();

		subselect(0, 1, result, k, t, a[1]);
		System.out.println((System.currentTimeMillis() - start)
				+ "=====end time====");
	}

	private static char OPEN_Str = a[0];
	private static boolean isBreak = false;

	private void subselect(int head, int index, char[] r, int k, T list,
			char end) {
		for (int i = head; i < a.length + index - k; i++) {

			// System.out.println(list+"-------------");
			if (isBreak) {
				// if (!pwdlist.isEmpty()) {
				// try {
				// writePwd();
				// break;
				// } catch (IOException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				// }
				break;

			}

			if (index < k) {
				r[index - 1] = a[i];
				subselect(i + 1, index + 1, r, k, null, ' ');
			} else if (index == k) {
				r[index - 1] = a[i];
				// String s = new String(r);
				if (r[0] != OPEN_Str)
					isBreak = true;
				else
					System.out.println(r);
				// pwdlist.add(new String(r));
				// if (pwdlist.size() == 2048)
				// if (!pwdlist.isEmpty()) {
				// try {
				// writePwd();
				// break;
				// } catch (IOException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				// }
				slens++;
				subselect(i + 1, index + 1, r, k, null, ' ');
			} else {
				return;
			}
			if (null != list && root.equals(list.toString()) && slens > 0) {
				elens = slens - elens;
				System.out.println(a[i] + "~~~~~" + elens);
				elens = slens;
			}

		}
	}

	static final String nn = "\n";

	public static void writePwd() throws IOException {
		if (!pwdlist.isEmpty())
			for (String s : pwdlist)
				bw.write(s + nn);
		bw.flush();
		pwdlist.clear();
		// bw.close();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String path = null;
		int startChar = 0;
		int pwdLen = 0;
		if (null == path)
			System.out.println();
		if (null != args && args.length > 0) {
			path = args[0];
			pwdLen = Integer.parseInt(args[1]);
			startChar = Integer.parseInt(args[2]);
		} else {
			path = "/Users/yanwei/Desktop/demoandroid/pwd1.txt";
		}
		System.out.println("=======统计密码开始=================");
		PwdCreator pwd = null;
		try {
			pwd = new PwdCreator(path);
			// for(int i=0;i<a.length;i++)
			pwd.select(pwdLen, startChar);
			close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println("=======统计密码结束=================");
	}

}
